// pages/Shop/Search/Search.js
const Net = require('../../../../Net');
const http = require('../../../../utils/http.js');
Page({
  data: {
    index: 0,
    inputVal: '',
    searchRecord: [],
    index: 1,
    indexs: 1,
    hide: true,
    imageurl2: 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png',
    imageurl3: 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png',
    // 列表显示
    showlist: false,
    tabArr: {
      curHdIndex: 0,
      curBdIndex: 0
    },
    listx: [{
      coupon_info_money: "0",
      format_volume: "7人付款",
      pict_url: "https://img.alicdn.com/imgextra/i2/1712484042/TB18qZMoJbJ8KJjy1zjXXaqapXa_!!0-item_pic.jpg_430x430q90.jpg",
      quanhou_jiage: "44.50",
      shop_title: "快乐英语启蒙书...",
      size: "44.50",
      tao_id: 92772343142,
      title: "焙宁28天质保乳酸菌小口袋非常好吃到爆炸",
      tkfee3: "6.67",
      user_type: 2,
      user_type_logo: "https://api.huixuangou.com.cn/uploads/icon/pdd.jpg",
      user_type_price: "售价44.50",
      user_type_price_name: ""
    }],
    pageScroll: 0,
    showHidden: false,
    focus: true,
    onList: [{
      id: 0,
      type: '综合',
      onClickStatus: false,
      onClickImg: '',
      statusType: 'new'
    }, {
      id: 1,
      type: '销量',
      onClickStatus: false,
      onClickImg: 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png',
      statusType: ''
    }, {
      id: 2,
      type: '佣金',
      onClickStatus: false,
      onClickImg: 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png',
      statusType: ''
    }, {
      id: 3,
      type: '价格',
      onClickStatus: false,
      onClickImg: 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png',
      statusType: ''
    }]
  },
  onSeareList(e) {
    var id = e.currentTarget.id,
      list = Net.data('onList'),
      data = {},
      _this = this
    if (id == 0) {
      list[0].onClickStatus = true
      data.sort = 0
    } else {
      list[0].onClickStatus = false
    }
    for (let i in list) {
      if (list[i].id == id && list[i].id != 0) {
        if (list[i].onClickStatus) {
          if (list[i].statusType == 'sale_num_asc') {
            list[i].statusType = 'sale_num_desc'
            list[i].onClickImg = 'http://oss.myzy.com.cn/wechat/images/icon_xq_x.png'
            data.sort = 6
          } else if (list[i].statusType == 'sale_num_desc') {
            list[i].statusType = 'sale_num_asc'
            list[i].onClickImg = 'http://oss.myzy.com.cn/wechat/images/icon_xq_s.png'
            data.sort = 5
          } else if (list[i].statusType == 'commission_rate_asc') {
            list[i].statusType = 'commission_rate_desc'
            list[i].onClickImg = 'http://oss.myzy.com.cn/wechat/images/icon_xq_x.png'
            data.sort = 14
          } else if (list[i].statusType == 'commission_rate_desc') {
            list[i].statusType = 'commission_rate_asc'
            data.sort = 13
            list[i].onClickImg = 'http://oss.myzy.com.cn/wechat/images/icon_xq_s.png'
          } else if (list[i].statusType == 'price_asc') {
            list[i].statusType = 'price_desc'
            list[i].onClickImg = 'http://oss.myzy.com.cn/wechat/images/icon_xq_x.png'
            data.sort = 4
          } else if (list[i].statusType == 'price_desc') {
            list[i].statusType = 'price_asc'
            list[i].onClickImg = 'http://oss.myzy.com.cn/wechat/images/icon_xq_s.png'
            data.sort =3
          } else {
            list[i].statusType = ''
            list[i].onClickStatus = false
            list[i].onClickImg = 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png'
          }
        } else {
          if (list[i].id == 1) {
            list[i].statusType = 'sale_num_desc'
            list[i].onClickImg = 'http://oss.myzy.com.cn/wechat/images/icon_xq_x.png'
            list[i].onClickStatus = true
            data.sort = 6
          } else if (list[i].id == 2) {
            list[i].statusType = 'commission_rate_desc'
            list[i].onClickImg = 'http://oss.myzy.com.cn/wechat/images/icon_xq_x.png'
            list[i].onClickStatus = true
            data.sort = 14
          } else if (list[i].id == 3) {
            list[i].statusType = 'price_desc'
            list[i].onClickImg = 'http://oss.myzy.com.cn/wechat/images/icon_xq_x.png'
            list[i].onClickStatus = true
            data.sort = 4
          } else {
            list[i].statusType = ''
            list[i].onClickStatus = false
            list[i].onClickImg = 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png'
          }
        }
      } else if (list[i].id != 0) {
        list[i].onClickStatus = false
      }
    }
    Net.data({
      onList: list
    })
    data.keyword = Net.data('searchValue')
    data.now_page = Net.data('now_page')
    data.mall_type = 'pdd'
    console.log(data)
    Net.link('v1/searchGoods', data, function (res) {
      console.log(res)
      wx.hideToast()
      const list = res.data.data.list
      _this.setData({
        list: list
      })
      if (res.data.data.list !== '') {
        _this.setData({
          scroll_state: 1
        })
      }
    })
  },
  // 选项卡切换
  tabbar(e) {
    this.setData({
      index: e.currentTarget.dataset.index
    })
  },
  openHistorySearch: function () {
    this.setData({
      searchRecord: wx.getStorageSync('searchRecord') || [], //若无储存则为空
    })
  },
  //点击历史记录搜索
  GetList(e) {
    console.log(e)
    const value = e.currentTarget.dataset.value;
    const data = {
      keyword: value,
      now_page: 1,
      mall_type: 'pdd',
      sort: 'new',
      youquan: 0
    }
    this.searchGoods(data)
    this.setData({
      showlist: true,
      searchValue: value
    })
  },
  onGoodsInfo(e) {
    var _this = this,
      list = _this.data.list,
      DS = e.currentTarget.dataset.index
    wx.setStorageSync('detail', list[DS])
    wx.navigateTo({
      url: '/pages/onNewPages/Shop/Detail/Detail',
    })
  },
  //点击搜索按钮提交表单跳转并储存历史记录
  searchSubmitFn: function (e) {
    var that = this
    var inputVal = e.detail.value
    var searchRecord = that.data.searchRecord
    if (inputVal == '') {
      wx.showToast({
        title: '搜索内容不能为空',
        icon: 'none'
      })
    } else {
      const data = {
        keyword: inputVal,
        now_page: 1,
        mall_type: 'pdd',
        sort: 'new',
        youquan: 0
      }
      this.searchGoods(data)
      this.setData({
        showlist: true,
        searchValue: inputVal
      })
      //将搜索值放入历史记录中,只能放前五条
      if (searchRecord.length < 5) {
        searchRecord.unshift(inputVal)
        this.unique(searchRecord)
      } else {
        searchRecord.pop() //删掉旧的时间最早的第一条
        searchRecord.unshift(inputVal)
        this.unique(searchRecord)
      }
      //将历史记录数组整体储存到缓存中
      wx.setStorageSync('searchRecord', this.unique(searchRecord))
    }
  },
  unique(arr) {
    return Array.from(new Set(arr))
  },
  historyDelFn: function () {
    wx.clearStorageSync('searhRecord')
    this.setData({
      searchRecord: []
    })
  },
  keyword(e) {
    if (e.detail.value != '') {
      this.setData({
        hide: false
      })
    } else {
      this.setData({
        hide: true
      })
    }
  },
  Del() {
    this.setData({
      hide: false,
      searchValue: ''
    })
  },
  // 搜索列表
  searchGoods(data) {
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    var _this = this,
      info = _this.data.list
    http.postRequest('v1/searchGoods', data,
      function (res) {
        wx.hideToast()
        const list = res.data.list
        _this.setData({
          list: data.now_page == 1 ? list : info.concat(list),
          now_page: res.data.page
        })
        if (res.data.list !== '') {
          _this.setData({
            scroll_state: 1
          })
        } else {
          wx.stopPullDownRefresh()
          _this.setData({
            scroll_state: 0
          })
        }
      },
      function (err) {
        _this.toasts.hideToast({})
        _this.toastDialog.showDialog(err.msg)
      })
  },
  onPageScroll(e) {
    var _this = this,
      pageScroll = _this.data.pageScroll;
    console.log(pageScroll)
    if (e.scrollTop < 200) {
      _this.setData({
        pageScroll: e.scrollTop
      })
    } else {
      if (pageScroll > e.scrollTop) {
        _this.setData({
          showHidden: false,
          pageScroll: e.scrollTop
        })
      } else {
        _this.setData({
          showHidden: true,
          pageScroll: e.scrollTop
        })
      }
    }

  },
  onLoad: function (options) {
    var that = this
    const searchValues = options.search_txt
    this.setData({
      searchValues
    })
    this.openHistorySearch()
    var query = wx.createSelectorQuery();
    query.select('#tabar').boundingClientRect(function (rect) {
      console.log(rect)
      that.setData({
        height: rect.height + 'px',
        listTop: rect.height + 8 + 'px',
      })
    }).exec();
  },
  onReady: function () {
    this.toastDialog = this.selectComponent("#toastDialog"); //此处id和页面的 wxml中的id一致
    this.toasts = this.selectComponent('#toasts')
  },
  onShow: function () {},
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {
    let page = this.data.now_page;
    page++;
    const data = {
      keyword: this.data.searchValue,
      now_page: page,
      mall_type: 'pdd',
      sort: 'new',
      youquan: 0
    }
    console.log(data)
    this.searchGoods(data)
  },
  onShareAppMessage: function () {},
  tabFun: function (e) {
    var _datasetId = e.target.dataset.id;
    switch (_datasetId) {
      // 综合
      case "0":
        this.setData({
          imageurl3: 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png',
          imageurl2: 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png',
          index: 1,
          indexs: 1
        })
        break;
        //优惠券
      case "3":
        this.setData({
          imageurl3: 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png',
          imageurl2: 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png',
          index: 1,
          indexs: 1,
        })
        break;
    }
    var _obj = {};
    _obj.curHdIndex = _datasetId;
    _obj.curBdIndex = _datasetId;
    this.setData({
      tabArr: _obj
    });
  },
  // 价格排序
  changechoose: function (e) {
    console.log('55')
    this.setData({
      imageurl3: 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png',
      indexs: 1
    })
    var index = e.target.dataset.index;
    var that = this;
    if (index == 1) {
      that.setData({
        index: 2,
        imageurl2: 'http://oss.myzy.com.cn/wechat/images/icon_xq_s.png',
      })

    } else if (index == 2) {
      that.setData({
        index: 1,
        imageurl2: 'http://oss.myzy.com.cn/wechat/images/icon_xq_x.png',
      })
    }
  },
  // 佣金排序
  changechooses: function (e) {
    this.setData({
      imageurl2: 'http://oss.myzy.com.cn/wechat/images/icon_xq_h.png',
      index: 1
    })
    var index = e.target.dataset.index;
    var that = this;
    if (index == 1) {
      that.setData({
        indexs: 2,
        imageurl3: 'http://oss.myzy.com.cn/wechat/images/icon_xq_s.png',
      })
    } else if (index == 2) {
      that.setData({
        indexs: 1,
        imageurl3: 'http://oss.myzy.com.cn/wechat/images/icon_xq_x.png',
      })
    }
  },
})